package com.jckj.core.controller;
/**
 * Copyright &copy; 2018-2018 <a href="#">聚成科技</a> All rights reserved.
 */

import com.github.pagehelper.PageInfo;
import com.jckj.core.entity.CmsArticle;
import com.jckj.core.service.CmsArticleService;
import com.jckj.core.util.StringUtils;
import com.jckj.core.validator.groups.Add;
import com.jckj.core.validator.groups.Delete;
import com.jckj.core.validator.groups.View;
import com.jckj.core.vos.LoginUserVo;
import com.jckj.core.vos.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;

/**
 * <p>
 * 内容表的控制器
 * </p>
 *
 * @author :liukuan
 * @date :2018-01-26
 */
@Controller
@RequestMapping("/admin")
public class CmsArticleController {

    @Autowired
    private CmsArticleService cmsArticleService;
    @Autowired
    private LoginUserVo loginUserVo;

    /**
     * 通配符的写法，根据url传过来的数据找到相对应的页面
     *
     * @param page
     * @return
     */
    @RequestMapping(value = "/{page}", method = RequestMethod.GET)
    public String page(@PathVariable String page) {
        return page;
    }


    /**
     * （categoryId）查询相关的内容，并且带分页
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/findList", method = RequestMethod.POST)
    @ResponseBody
    public Result findCmsArticle(@ModelAttribute @Validated(value = View.class) CmsArticle cmsArticle,
                                 BindingResult bindingResult,
                                 @RequestParam(required = true, defaultValue = "1") Integer pageNum,
                                 @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        if (bindingResult.hasErrors()) {
            return Result.invalidParams(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        PageInfo<CmsArticle> info = cmsArticleService.findByCategoryId(cmsArticle.getCategoryId(), pageNum, pageSize);
        if (info.getList().size() > 0) {
            return Result.ok(info);
        }
        return Result.error("执行错误");
    }

    /**
     * 添加内容的方法
     *
     * @param cmsArticle
     * @param attachmentId
     * @param
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public Result insert(@ModelAttribute @Validated(value = Add.class) CmsArticle cmsArticle,
                         @RequestParam(required = true) String attachmentId,
                         BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.invalidParams(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }

        cmsArticle.setId(StringUtils.genId());
        cmsArticle.setCreateBy(loginUserVo.getId() == null ? null : loginUserVo.getId());
        cmsArticle.setCreateTime(Calendar.getInstance().getTime());
        int num = cmsArticleService.insert(attachmentId, cmsArticle);
        if (num > 0) {
            return Result.ok();
        }
        return Result.error("执行错误");
    }

    /**
     * 删除内容的方法，并把附件表的对关系也删掉
     *
     * @param
     * @return
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@ModelAttribute @Validated(value = Delete.class) CmsArticle cmsArticle,
                         BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.invalidParams(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        int num = cmsArticleService.delete(cmsArticle.getId());
        if (num > 0) {
            return Result.ok();
        }
        return Result.error("执行错误");
    }

    /**
     * 修改的方法
     *
     * @param cmsArticle
     * @param attachmentId
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Result update(@ModelAttribute @Validated(value = Add.class) CmsArticle cmsArticle,
                         @RequestParam(required = false) String attachmentId,
                         BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.invalidParams(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        cmsArticle.setUpdateBy(loginUserVo.getId() == null ? null : loginUserVo.getId());
        cmsArticle.setUpdateTime(Calendar.getInstance().getTime());
        int num = cmsArticleService.update(attachmentId, cmsArticle);
        if (num > 0) {
            return Result.ok();
        }
        return Result.error("执行错误");
    }


}
